package xyz.ljcc.ccmt.handler.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import xyz.ljcc.ccmt.model.base.Operation;
import xyz.ljcc.ccmt.model.result.ResultModel;
import xyz.ljcc.ccmt.service.api.admin.OperationService;

/**
 * 菜单请求头部分
 * @author liucan
 *
 */
@RequestMapping("/admin/operation")
@RestController
public class OperationHandler {

	@Autowired
	private OperationService operationService;
	
	@PostMapping(value="/parent/{pid}")
	public ResultModel loadParentModule(@PathVariable(value="pid") Integer parentId) {
		List<Operation> operations = operationService.loadOperationsByParentId(parentId);
		return new ResultModel(true, operations, "获取子级菜单成功");
	}
	
	@PostMapping(value="loadParentModule")
	public ResultModel loadParentOperations() {
		List<Operation> operations = operationService.loadParentOperations(); 
		return new ResultModel(true, operations, "获取父级菜单成功");
	}
	
	@GetMapping(value="/{operationId}")
	public ResultModel getOperationById(@PathVariable(value="operationId",required=true) Integer operationId) {
		Operation operation = operationService.getOperationById(operationId); 
		return new ResultModel(true, operation, "获取单条记录成功");
	}
	
	@PostMapping(value="/loadModuleList")
	public ResultModel loadOperations() {
		List<Operation> operations = operationService.loadOperations();
		return new ResultModel(true, operations, "查询所有菜单成功");
	}
	
	
	@PostMapping
	public ResultModel saveModule(Operation operation) {
		operationService.addOperation(operation);
		return new ResultModel(true, null, "新增成功");
	}
	
	@PostMapping(value="/parent")
	public ResultModel saveParentModule(Operation operation) {
		operationService.addOperation(operation);
		return new ResultModel(true, null, "新增成功");
	}
	
	@PutMapping
	public ResultModel editOperation(Operation operation) {
		operationService.editOperation(operation);
		return new ResultModel(true, null, "更新成功");
	}
	
	@DeleteMapping(value="/{operationId}")
	public ResultModel delModule(@PathVariable(value="operationId",required=true)  Integer operationId) {
		operationService.delOperation(operationId);
		return new ResultModel(true, null, "删除成功");
	}
	
	@PostMapping(value="/active")
	public ResultModel activeModule(Integer operationId,Integer active) {
		operationService.activeOperation(operationId, active);
		return new ResultModel(true, null, active == 0 ? "停用成功" : "启用成功");
	}
	
}
